package com.cskaoyan.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class CustomAspect {

    /**
     * 切入点的配置
     * value属性：切入点表达式
     * 方法名作为id
     */
    @Pointcut("execution(* com.cskaoyan.service..*(..))")
    public void pointcut1() {
    }

    //方法名任意写
    @Before("execution(* com..service..*(..))")
    public void before() {
        System.out.println("雪茄好");
    }
    @After("pointcut1()")
    public void after() {
        System.out.println("雪茄再见");
    }

    //类似于InvocationHandler的invoke(proxy,method,args)
    //类似于MethodInterceptor的invoke(methodInvocation)
    @Around("pointcut1()")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        //委托类方法的执行，around方法的执行结果：代理对象的结果
        System.out.println("around的前半部分");
        Object proceed = joinPoint.proceed();
        System.out.println("around的后半部分");

        return proceed;
    }

    //形参来接收委托类方法的返回结果
    @AfterReturning(value = "pointcut1()",returning = "result")
    public void afterReturning(Object result) {
        System.out.println("afterReturning接收的结果：" + result);
    }

    //形参来接收委托类方法抛出的异常 Exception或Throwable
    @AfterThrowing(value = "pointcut1()",throwing = "exception")
    public void afterThrowing(Exception exception) {
        System.out.println("afterThrowing:" + exception.getMessage());
    }
}
